package yunwo.cn.yzstation.model;

import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import yunwo.cn.yzstation.base.Yzapp;
import yunwo.cn.yzstation.bean.BatchNumberBean;
import yunwo.cn.yzstation.bean.CarOBJBean;
import yunwo.cn.yzstation.bean.ContinueBean;
import yunwo.cn.yzstation.bean.EndOrganbean;
import yunwo.cn.yzstation.view.activity.AssignmentActivity;
import yunwo.cn.yzstation.api.HttpManager;
import yunwo.cn.yzstation.api.IassigmentModel;
import yunwo.cn.yzstation.api.okapi.GLCallBack;
import yunwo.cn.yzstation.bean.CarBean;
import yunwo.cn.yzstation.bean.DriverBean;
import yunwo.cn.yzstation.config.MyConstants;
import yunwo.cn.yzstation.utils.StringUtil;

/**
 * Created by lao on 2017/10/11.
 */
public class AssigmentModel {
    AssignmentActivity assignmentActivity;
    IassigmentModel iassigmentModel;
    private List<DriverBean.DataBean> mDriverBeanList;
    private List<CarBean.DataBean> mCarBeanList;
    public AssigmentModel(AssignmentActivity assignmentActivity, IassigmentModel iassigmentModel) {
        this.assignmentActivity = assignmentActivity;
        this.iassigmentModel = iassigmentModel;

    }

    public void requestPopupCon(String mycarnumber) {
        Map<String, Object> map = new HashMap<>();
        map.put("platNumber", mycarnumber);
        HttpManager.hpptPostString3(MyConstants.getVehicles, map).execute(new GLCallBack() {



            @Override
            public void onResponseT(String result, int code, String msg, int id) {
                if (code == 200) {
                    if (!StringUtil.isEmpty(result)) {
                        mCarBeanList = new Gson().fromJson(result, new TypeToken<List<CarBean.DataBean>>() {
                        }.getType());
                        iassigmentModel.getCarPopupConSueecss(mCarBeanList);
                    }
                }
            }

            @Override
            public void onError(Call call, Exception e, int id) {

            }
        });
    }

    public void requestDriverPopupCon(String mydriver, String driverphone) {
        Map<String, Object> map = new HashMap<>();
        map.put("name", mydriver);
        map.put("phone", driverphone);
        HttpManager.hpptPostString3(MyConstants.driverVehicle, map).execute(new GLCallBack() {


            @Override
            public void onResponseT(String result, int code, String msg, int id) {
                if (code == 200) {
                    if (!StringUtil.isEmpty(result)) {
                        mDriverBeanList = new Gson().fromJson(result, new TypeToken<List<DriverBean.DataBean>>() {
                        }.getType());
                        iassigmentModel.getDriverPopupConSueecss(mDriverBeanList);
                    }
                } else {
                    Toast.makeText(assignmentActivity, msg, Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError(Call call, Exception e, int id) {

            }
        });
    }

    public void requestDriverPhonePopupCon(String mydriverphone, String name) {
        Map<String, Object> map = new HashMap<>();
        map.put("phone", mydriverphone);
        map.put("name", name);
        Log.d("dsfs", mydriverphone);

        HttpManager.hpptPostString3(MyConstants.driverVehicle, map).execute(new GLCallBack() {
            @Override
            public void onResponseT(String result, int code, String msg, int id) {
                if (code == 200) {
                    if (!StringUtil.isEmpty(result)) {
                        mDriverBeanList = new Gson().fromJson(result, new TypeToken<List<DriverBean.DataBean>>() {
                        }.getType());
                        iassigmentModel.getDriverPopupConSueecss(mDriverBeanList);
                    }
                }else {
                    Toast.makeText(assignmentActivity, msg, Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError(Call call, Exception e, int id) {

            }
        });
    }

    public void getLoadingobj() {

        Map<String, Object> map = new HashMap<>();

        map.put("organName", "");

      HttpManager.hpptPostString3(MyConstants.loading,map).execute(new GLCallBack() {

          private List<CarOBJBean.DataBean> mDataBeanList;

          @Override
          public void onResponseT(String result, int code, String msg, int id) {
              if (code == 200) {
                  if (!StringUtil.isEmpty(result)) {
                      mDataBeanList = new Gson().fromJson(result, new TypeToken<List<CarOBJBean.DataBean>>() {
                      }.getType());
                      iassigmentModel.getLoadingobjSuccess(mDataBeanList);
                  }
              }
          }

          @Override
          public void onError(Call call, Exception e, int id) {

          }
      });
    }

    public void getBatchNumber() {

        Map<String, String> map = new HashMap<>();
        map.put("params", "");
        HttpManager.hpptPostString(MyConstants.getBatchNumber,map).execute(new GLCallBack() {

            private BatchNumberBean.DataBean mDataBean;

            @Override
            public void onResponseT(String result, int code, String msg, int id) {
                if (code == 200) {
                    if (!StringUtil.isEmpty(result)) {
                        mDataBean = new Gson().fromJson(result, BatchNumberBean.DataBean.class);
                        iassigmentModel.getBachNumberSuccess(mDataBean);
                    }
                }else {
                    Toast.makeText(assignmentActivity, msg, Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError(Call call, Exception e, int id) {
                Toast.makeText(assignmentActivity, "获取批次号失败", Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void getContinueData(String aContinue) {
        Map<String, Object> map = new HashMap<>();
        map.put("departId", aContinue);
        HttpManager.hpptPostString3(MyConstants.proceed,map).execute(new GLCallBack() {

            private ContinueBean.DataBean mDataBean;

            @Override
            public void onResponseT(String result, int code, String msg, int id) {
                if (code == 200) {
                    if (!StringUtil.isEmpty(result)) {
                        mDataBean = new Gson().fromJson(result, ContinueBean.DataBean.class);
                        iassigmentModel.getContinueDataSuccess(mDataBean);
                    }
                }else {
                    Toast.makeText(assignmentActivity, msg, Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError(Call call, Exception e, int id) {
                Toast.makeText(assignmentActivity, "获取数据失败", Toast.LENGTH_SHORT).show();

            }
        });
    }

    public void getEndOrgan() {

        HashMap<String, String> map = new HashMap<>();
        map.put("params", Yzapp.getInstance().getBeginOrgId());
        HttpManager.hpptPostString(MyConstants.EndOrgan, map).execute(new GLCallBack() {
            @Override
            public void onResponseT(String result, int code, String msg, int id) {
                if (code == 200) {
                    if (!result.equals("")) {
                        List<EndOrganbean.DataBean> dataBeanList = new Gson().fromJson(result, new TypeToken<List<EndOrganbean.DataBean>>() {
                        }.getType());
                        iassigmentModel.getEndOrganSuccess(dataBeanList);
                        Log.d("xxxxxxxxx", dataBeanList.size() + "");

                    }
                }else {
                    iassigmentModel.getEndOrganfailure();
                    Toast.makeText(assignmentActivity, msg, Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError(Call call, Exception e, int id) {
                iassigmentModel.getEndOrganfailure();
                Toast.makeText(assignmentActivity, "服务器异常", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
